import { vendorsFmtDate } from './../../../utils/config';
import moment from 'moment';
moment.locale('zh-cn');

export default function (attrcode, oldValue, value) {
    if(attrcode === 'scode') {
        this.props.form.setFormItemsValue({
            'sname': { value: value.record && value.record.sname},
            'pkProject': { value: value.record && value.record.pkProject },
            'orgName': { value: value.record && value.record.orgName }
           });
    }
    if(attrcode === 'stopDate') {
        let stopDateValue = Date.parse(value.value);
        let preresumeDate = this.props.form.getFormItemValue('preresumeDate');
        if(preresumeDate.value) {
            let preresumeDateValue = Date.parse(preresumeDate.value);
            let workstopdays = preresumeDateValue - stopDateValue;
            let iDays = Math.floor(workstopdays / (24 * 3600 * 1000));
            this.props.form.setFormItemsValue({
                'workstopdays': { value: isNaN(iDays) ? '' : iDays }
            });
        }
    } else if(attrcode === 'preresumeDate') {
        let preresumeDateValue = Date.parse(value.value);
        let stopDate = this.props.form.getFormItemValue('stopDate').value;
        let stopDateValue = Date.parse(stopDate);
        let workstopdays = preresumeDateValue - stopDateValue;
        if (workstopdays < 0) {
            this.props.form.setFormItemsValue({
                'preresumeDate': oldValue
            });
        }else{
            let iDays = Math.floor(workstopdays / (24 * 3600 * 1000));
            this.props.form.setFormItemsValue({
                'workstopdays': {value: isNaN(iDays) ? '' : iDays}
            });
        }
    } 
    else if(attrcode === 'workstopdays') {
        value = value.value.replace(/[^0-9]/ig, '');
        let workstopdays = value * (24 * 3600 * 1000);
        let stopDate = this.props.form.getFormItemValue('stopDate');
        let stopDateValue = Date.parse(stopDate.value);
        let preresumeDate = stopDateValue + workstopdays;
        let preresumeDateValue = vendorsFmtDate(preresumeDate);
        this.props.form.setFormItemsValue({
            'preresumeDate': {value: moment(preresumeDateValue)},
            'workstopdays': {value: value}
        });
    }
}